//
//  ReadPageView_Memo.m
//  KidReading
//
//  Created by telen on 16/4/26.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "ReadPageView_Memo.h"
#import "ConstDefine.h"

@implementation MemoButton
@end


@interface ReadPageView_Memo()<UIGestureRecognizerDelegate>
@end

@implementation ReadPageView_Memo

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSelf:)]];
        _delMemoBtnList = [NSMutableArray new];
        _modyMemoBtnList = [NSMutableArray new];
        _syncMemoBtnList = [NSMutableArray new];
    }
    return self;
}

- (void)clearSyncMemoBtns
{
    [_syncMemoBtnList enumerateObjectsUsingBlock:^(MemoButton*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [_syncMemoBtnList removeAllObjects];
}

- (MemoButton *)addMemoBtnWith_pt:(CGPoint)pt text:(NSString *)text creatTime:(long long)cttime modyTime:(long long)mdtime
{
    CGFloat ratio_w = [DataProcessing shareSingle].ratio_w;
    MemoButton* btn = [MemoButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 50/ratio_w, 50/ratio_w);
    btn.center = pt;
    btn.oldTxt = text;
    btn.txt = text;
    btn.createTime = cttime;
    btn.modyTime = mdtime;
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_pushpin_normal.png"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_pushpin_pressed.png"] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_pushpin_pressed.png"] forState:UIControlStateSelected];
    [self addSubview:btn];
    [btn addTarget:self action:@selector(clicked_MemoBtn:) forControlEvents:UIControlEventTouchUpInside];
    __weak ReadPageView_Memo* wself = self;
    UIPanGestureRecognizer* pan = [btn addPanGesture_Move_End:^(UIView *view, CGPoint offset) {
        [wself list_ModyBtn:(MemoButton*)view];
    }];
    pan.delegate = self;
    return btn;
}

- (void)tapSelf:(UITapGestureRecognizer*)tap
{
    ReadBookVOrH_VC* rvc = [ReadBookVOrH_VC currentReadInstance];
    NSNumber* unshowMemo = [NSObject msgToTarget:rvc menthed:@"didTapSelfView_unShowMemoView" refs:nil needReturnValue:YES];
    if (unshowMemo != nil) {
        return;
    }
    UIView* view = tap.view;
    CGPoint pt = [tap locationInView:view];
    long long time = [DataProcessing timestampFromNSDate:[NSDate date]];
    MemoButton* btn = [self addMemoBtnWith_pt:pt text:nil creatTime:time modyTime:time];
    [[NSNotificationCenter defaultCenter] postNotificationName:MemoButtonEvent_AddNewMemoBtn object:self userInfo:@{@"mbtn":btn}];
}

- (void)clicked_MemoBtn:(MemoButton*)mbtn
{
    mbtn.isEditModel = self.editModel;
    if(mbtn)[[NSNotificationCenter defaultCenter] postNotificationName:MemoButtonEvent_TouchUpInSile object:self userInfo:@{@"mbtn":mbtn}];
}

#pragma mark-
- (void)list_ModyBtn:(MemoButton*)mbtn
{
    BOOL forNew = YES;
    for (MemoButton* btn in self.modyMemoBtnList) {
        if (btn == mbtn) {
            forNew = NO;
            break;
        }
    }
    if (forNew) {
        [self.modyMemoBtnList addObject:mbtn];
    }
    mbtn.modyTime = [DataProcessing timestampFromNSDate:[NSDate date]];
}

- (void)list_DelBtn:(MemoButton*)mbtn
{
    NSArray* arr = [NSArray arrayWithArray:self.modyMemoBtnList];
    for (MemoButton* btn in arr) {
        if (btn == mbtn) {
            [self.modyMemoBtnList removeObject:btn];
            break;
        }
    }
    arr = [NSArray arrayWithArray:self.syncMemoBtnList];
    for (MemoButton* btn in arr) {
        if (btn == mbtn) {
            [self.syncMemoBtnList removeObject:btn];
            break;
        }
    }
    [self.delMemoBtnList addObject:mbtn];
}

#pragma mark-
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gesture
{
    if (self.editModel) {
        return YES;
    }
    return NO;
}

@end
